﻿using Guwave.MonitorDevice.Common;
using Guwave.MonitorDevice.WPFClientApp.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml;

namespace Guwave.MonitorDevice.WPFClientApp.Utility
{
    public class ConfigManager
    {
        private MonitorDeviceModel _monitorDeviceMode;
        private ObservableCollection<Visibility> _groupBoxVisibilities;

        public ConfigManager(MonitorDeviceModel monitorDeviceModel, ObservableCollection<Visibility> groupBoxVisibilities)
        {
            _monitorDeviceMode = monitorDeviceModel;
            _groupBoxVisibilities = groupBoxVisibilities;
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="obj"></param>
        public  void SaveConfig()
        {
            XmlDocument xmlDoc = new XmlDocument();
            string configPath = System.AppDomain.CurrentDomain.BaseDirectory + "\\Config\\DrivcesInfo.xml";
            xmlDoc.Load(configPath);
            XmlNode xnRoot = xmlDoc.SelectSingleNode("ConfigInfo");
            if (_groupBoxVisibilities[0] == Visibility.Visible)
            {
                XmlNode infanceArrdessNode = xnRoot.SelectSingleNode("InfanceArrdess");
                XmlElement xe = (XmlElement)infanceArrdessNode;
                xe.SetAttribute("Address", _monitorDeviceMode.InfanceArrdess);
            }
            else if (_groupBoxVisibilities[1] == Visibility.Visible)
            {
                XmlNode deviceIntervalTimeNode = xnRoot.SelectSingleNode("DeviceIntervalTime");
                XmlElement xe = (XmlElement)deviceIntervalTimeNode;
                xe.SetAttribute("IntervalTime", _monitorDeviceMode.ReadDeviceIntervalTime);

                XmlNode drivcesInfoNode = xnRoot.SelectSingleNode("DrivcesInfo");
                XmlNodeList drivceNodeList = drivcesInfoNode.ChildNodes;
                foreach (XmlNode xmlNode in drivceNodeList)
                {
                    XmlElement xe2 = (XmlElement)xmlNode;
                    xe2.SetAttribute("ActivePoint", _monitorDeviceMode.DefaultPoint);
                    xe2.SetAttribute("ActiveReadBuffer", _monitorDeviceMode.DefaultReadBuffer);
                }
            }
            else if (_groupBoxVisibilities[2] == Visibility.Visible)
            {
                XmlNode deviceIntervalTimeNode = xnRoot.SelectSingleNode("FileIntervalTime");
                XmlElement xe = (XmlElement)deviceIntervalTimeNode;
                xe.SetAttribute("IntervalTime", _monitorDeviceMode.ReadFileIntervalTime);
                XmlNode filePathNode = xnRoot.SelectSingleNode("FilePath");
                XmlElement xe2 = (XmlElement)filePathNode;
                xe.SetAttribute("Path", _monitorDeviceMode.FilePath);
            }
            xmlDoc.Save(configPath);
        }

        /// <summary>
        /// 得到配置的值
        /// </summary>
        /// <param name="configInfoEnum"></param>
        /// <param name="attributeName"></param>
        public void GetConfigValue(ConfigInfo configInfoEnum, string attributeName = "")
        {
            XmlDocument xmlDoc = new XmlDocument();
            string configPath = System.AppDomain.CurrentDomain.BaseDirectory + "\\Config\\DrivcesInfo.xml";
            xmlDoc.Load(configPath);
            XmlNode xnRoot = xmlDoc.SelectSingleNode("ConfigInfo");
            if (configInfoEnum == ConfigInfo.InfanceArrdess)
            {
                XmlNode infanceArrdessNode = xnRoot.SelectSingleNode("InfanceArrdess");
                XmlElement xe = (XmlElement)infanceArrdessNode;
                _monitorDeviceMode.InfanceArrdess = xe.GetAttribute("Address");
            }
            else if (configInfoEnum == ConfigInfo.DeviceIntervalTime)
            {
                XmlNode deviceIntervalTimeNode = xnRoot.SelectSingleNode("DeviceIntervalTime");
                XmlElement xe = (XmlElement)deviceIntervalTimeNode;
                _monitorDeviceMode.ReadDeviceIntervalTime = xe.GetAttribute("IntervalTime");
            }
            else if (configInfoEnum == ConfigInfo.FileIntervalTime)
            {
                XmlNode fileIntervalTimeNode = xnRoot.SelectSingleNode("FileIntervalTime");
                XmlElement xe = (XmlElement)fileIntervalTimeNode;
                _monitorDeviceMode.ReadFileIntervalTime = xe.GetAttribute("IntervalTime");
                _monitorDeviceMode.FilePath = xe.GetAttribute("Path");
            }
            else if (configInfoEnum == ConfigInfo.DevicesInfo)
            {
                XmlNode drivcesInfoNode = xnRoot.SelectSingleNode("DrivcesInfo");
                XmlNodeList drivceInfoNodeList = drivcesInfoNode.ChildNodes;
                string values = string.Empty;
                foreach (XmlNode node in drivceInfoNodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    values = xe.GetAttribute(attributeName);
                }
                if (attributeName == "Points")
                {
                    _monitorDeviceMode.Points = new ObservableCollection<string>();
                    foreach (var point in values.Split(','))
                    {
                        _monitorDeviceMode.Points.Add(point);
                    }
                    _monitorDeviceMode.DefaultPoint = values.Split(',')[0];
                }
                else if (attributeName == "ReadBuffers")
                {
                    _monitorDeviceMode.ReadBuffers = new ObservableCollection<string>();
                    foreach (var readBuffer in values.Split(','))
                    {
                        _monitorDeviceMode.ReadBuffers.Add(readBuffer);
                    }
                    _monitorDeviceMode.DefaultReadBuffer = values.Split(',')[0];
                }
            }
        }
    }
}
